package sudokuTest;

import java.util.ArrayList;
import java.util.LinkedList;
import junit.framework.Assert;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import sudoku.ClassicCell;
import static org.junit.Assert.*;

/**
 *
 * @author taras
 */
public class ClassicCellTest {
    
    ClassicCell instance;
    int maxValue;
    
    public ClassicCellTest() {
    }

    @BeforeClass
    public static void setUpClass() throws Exception {
    }

    @AfterClass
    public static void tearDownClass() throws Exception {
    }
    
    @Before
    public void setUp() {
        maxValue = 9;
        instance = new ClassicCell(5,6,7,maxValue);
    }
    
    @After
    public void tearDown() {
    }

    /** Constructor test. */
    @Test
    public void testCell(){
        System.out.println("Cell");
        
        Assert.assertEquals(instance.getAccess(), true);
        Assert.assertEquals(instance.getRowPosition(), 5);
        Assert.assertEquals(instance.getColumnPosition(), 6);
        Assert.assertEquals(instance.getSqrPosition(), 7);
        Assert.assertEquals(instance.getValue(), 0);
        
        ArrayList<Integer> posVal = instance.getPossibleValues();
        for (int i=1; i<=maxValue; i++){
            Assert.assertTrue(posVal.contains(i));
        }
    }
    
    /**
     * Test of setValue method, of class ClassicCell.
     */
    @Test
    public void testSetValue() {
        System.out.println("setValue");
        
        int value = 5;
        Assert.assertTrue(instance.setValue(value));
        Assert.assertEquals(instance.getValue(), 5);
        
        value = 9;
        Assert.assertTrue(instance.setValue(value));
        Assert.assertEquals(instance.getValue(), 9);
        
        value = -555;
        Assert.assertFalse(instance.setValue(value));
        
        value = 0;
        Assert.assertFalse(instance.setValue(value));
        
        value = 10;
        Assert.assertFalse(instance.setValue(value));
    }

    /**
     * Test of eraseValue method, of class ClassicCell.
     */
    @Test
    public void testEraseValue() {
        System.out.println("eraseValue");
        
        instance.setValue(maxValue);
        instance.eraseValue();
        Assert.assertEquals(instance.getValue(), 0);
        
        ArrayList<Integer> posVal = instance.getPossibleValues();
        for (int i=1; i<=maxValue; i++){
            Assert.assertTrue(posVal.contains(i));
        }
    }

    /**
     * Test of getValue method, of class ClassicCell.
     */
    @Test
    public void testGetValue() {
        System.out.println("getValue");
        
        int i = 0;
        do{
            Assert.assertEquals(instance.getValue(), i);
            i++;
            instance.setValue(i);
        }while(i<=maxValue);
    }

    /**
     * Test of setAccess method, of class ClassicCell.
     */
    @Test
    public void testSetAccess() {
        System.out.println("setAccess");
        
        instance.setAccess(false);
        Assert.assertFalse(instance.getAccess());
        
        instance.setAccess(true);
        Assert.assertTrue(instance.getAccess());
    }

    /**
     * Test of getAccess method, of class ClassicCell.
     */
    @Test
    public void testGetAccess() {
        System.out.println("getAccess");
        
        Assert.assertTrue(instance.getAccess());
        instance.setAccess(false);
        Assert.assertFalse(instance.getAccess());
    }

    /**
     * Test of excludeFromPossibles method, of class ClassicCell.
     */
    @Test
    public void testExcludeFromPossibles() {
        System.out.println("excludeFromPossibles");
        
        ArrayList<Integer> posVal;
        for (int i=1; i<=maxValue; i++){            
            instance.excludeFromPossibles(i);
            posVal = instance.getPossibleValues();
            Assert.assertFalse(posVal.contains(i));
        }
    }

    /**
     * Test of compareTo method, of class ClassicCell.
     */
    @Test
    public void testCompareTo() {
        System.out.println("compareTo");
        
        ClassicCell c = new ClassicCell(5,6,7,maxValue);
        Assert.assertEquals(instance.compareTo(c), 0);
        
        c.setValue(5);
        Assert.assertEquals(instance.compareTo(c), -1);
        
        instance.setValue(4);
        Assert.assertEquals(instance.compareTo(c), 0);
        
        c.eraseValue();
        Assert.assertEquals(instance.compareTo(c), 1);
        
        instance.eraseValue();
        Assert.assertEquals(instance.compareTo(c), 0);
        
        c.excludeFromPossibles(1);
        Assert.assertEquals(instance.compareTo(c), 1);
        
        instance.excludeFromPossibles(1);
        instance.excludeFromPossibles(2);
         Assert.assertEquals(instance.compareTo(c), -1);
        
    }

    /**
     * Test of getRowPosition method, of class ClassicCell.
     */
    @Test
    public void testGetRowPosition() {
        System.out.println("getRowPosition");
        
        for (int i=0; i<maxValue; i++){
            instance = new ClassicCell(i,6,7,maxValue);
            Assert.assertEquals(instance.getRowPosition(), i);
        }
    }

    /**
     * Test of getColumnPosition method, of class ClassicCell.
     */
    @Test
    public void testGetColumnPosition() {
        System.out.println("getColumnPosition");
        
        for (int i=0; i<maxValue; i++){
            instance = new ClassicCell(5,i,7,maxValue);
            Assert.assertEquals(instance.getColumnPosition(), i);
        }
    }

    /**
     * Test of getSqrPosition method, of class ClassicCell.
     */
    @Test
    public void testGetSqrPosition() {
        System.out.println("getSqrPosition");
        
        for (int i=0; i<maxValue; i++){
            instance = new ClassicCell(5,6,i,maxValue);
            Assert.assertEquals(instance.getSqrPosition(), i);
        }
    }

    /**
     * Test of getPossibleValues method, of class ClassicCell.
     */
    @Test
    public void testGetPossibleValues() {
        System.out.println("getPossibleValues");
        
        ArrayList<Integer> posVal1 = instance.getPossibleValues();
        posVal1.clear();
        
        ArrayList<Integer> posVal2 = instance.getPossibleValues();
        Assert.assertFalse(posVal2.isEmpty());
        
        posVal2.add(0);
        ArrayList<Integer> posVal3 = instance.getPossibleValues();
        Assert.assertFalse(posVal3.contains(0));
    }

    /**
     * Test of addToPossibleValues method, of class ClassicCell.
     */
    @Test
    public void testAddToPossibleValues() {
        System.out.println("addToPossibleValues");
        
        Assert.assertFalse(instance.addToPossibleValues(0));
        Assert.assertFalse(instance.addToPossibleValues(maxValue+1));
        
        instance.setValue(maxValue);
        Assert.assertFalse(instance.addToPossibleValues(maxValue));
        
        instance.eraseValue();
        instance.excludeFromPossibles(maxValue);
        Assert.assertTrue(instance.addToPossibleValues(maxValue));
    }
    
    /** Test of clone method, of class ClassicCell. */
    @Test
    public void testClone(){
        System.out.println("clone");
        
        ClassicCell c = instance.clone();
        
        Assert.assertEquals(instance.getAccess(), c.getAccess());
        Assert.assertEquals(instance.getColumnPosition(), c.getColumnPosition());
        Assert.assertEquals(instance.getRowPosition(), c.getRowPosition());
        Assert.assertEquals(instance.getSqrPosition(), c.getSqrPosition());
        Assert.assertEquals(instance.getValue(), c.getValue());
        
        Assert.assertTrue(instance.getPossibleValues().containsAll(c.getPossibleValues()));
    
        c.setAccess(false);
        c.setValue(maxValue);
        Assert.assertFalse(instance.getAccess() == c.getAccess());
        Assert.assertFalse(instance.getValue() == c.getValue());
        Assert.assertFalse(c.getPossibleValues().containsAll(instance.getPossibleValues()));
    }
}
